package net.dfrz.lightshare.login.dao;

import org.hibernate.Query;
import org.hibernate.Session;

import net.dfrz.lightshare.MD5Util;
import net.dfrz.lightshare.SessionFactoryUtil;
import net.dfrz.lightshare.user.model.User;

public class LoginDaoImpl extends SessionFactoryUtil implements ILoginDao {
	
	
	@Override
	public User getUserByEmail(String email){
		
		Session session = factory.openSession();
		Query query = session.createQuery("from User user where user.email='"+email+"'");

		if(query.list().size()==1){
			return (User) query.uniqueResult();
		}
		return null;
	}
	@Override
	public User getUserByEmailAndPassword(User user){

		Session session = factory.openSession();

		Query query = session.createQuery("from User user where user.email='"+user.getEmail()+"' and user.password='"+MD5Util.MD5(user.getPassword())+"'");
		
		if(query.list().size()==1){
			return (User) query.uniqueResult();
		}
		return null;
	}
//	public static void main(String[] args) {
//		
//		User u = new User();
//		u.setEmail("453@qq.com");
//		u.setPassword("123");
//		LoginDaoImpl l = new LoginDaoImpl();
//		System.out.println(l.getUserByEmail("453@qq.com"));
//		System.out.println(l.getUserByEmailAndPassword(u));
//	}
}
